package com.sea.supply.service.impl;


import com.codecrane.core.util.CommonUtils;
import com.sea.data.entity.OrderDetail;
import com.sea.data.service.OrderDetailService;
import com.sea.physical.entity.CreateDistributeSku;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.service.CreateDistributeSkuService;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.supply.entity.*;
import com.sea.supply.mapper.BuyMapper;
import com.sea.supply.mapper.QualityReportMapper;
import com.sea.supply.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class QualityReportServiceImpl implements QualityReportService {

    @Autowired
    private QualityReportMapper qualityReportMapper;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private CreateDistributeSkuService createDistributeSkuService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(QualityReport qualityReport) {
        Integer[] ids = qualityReport.getIds();
        for (Integer id : ids) {
            CreatePlan createPlan = createPlanService.findById(id);
            //判断柜子是否被分配
            Integer createPlanStatus = createPlan.getCreatePlanStatus();
             if(createPlanStatus.equals(1)){
                throw new RuntimeException(createPlan.getCreatePlanNo()+"暂未分配,请先分配再质检！");
            }
            //查询改柜子下是否存在数据
             List<CreateDistributeSku> list = createDistributeSkuService.findByCreateIdSupplier(id,qualityReport.getSupplierName());
            if(null==list || list.size()==0){
                throw new RuntimeException(qualityReport.getSupplierName()+",暂未查询到该供应商数据！");
            }
            for(CreateDistributeSku distributeSku:list){
                //判断是否和供应商匹配
                if(qualityReport.getSupplierName().equals(distributeSku.getCreateSupplierName())){
                    distributeSku.setCreateQualityStatus(1);
                    //更新明细状态
                    createDistributeSkuService.updateQualityStatus(distributeSku);
                }
            }
            Integer number = createDistributeSkuService.findQualityNumber(id);

            List<CreateDistributeSku> numberList = createDistributeSkuService.findByCreateId(id);
            if(number==numberList.size()){
                //质检完成
                createPlan.setCreateQualityStatus(1);
                createPlanService.updateQualityStatus(createPlan);
            }else{
                //部分质检
                createPlan.setCreateQualityStatus(2);
                createPlanService.updateQualityStatus(createPlan);
            }
        }
        return qualityReportMapper.insert(qualityReport);
    }

    @Override
    public List<QualityReport> findByCnd(QualityReport qualityReport) {
        return qualityReportMapper.findByCnd(qualityReport);
    }

    @Override
    public Integer usave(QualityReport qualityReport) {
        return qualityReportMapper.update(qualityReport);
    }
}
